﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using HandyControl.Controls;

namespace IotClient.VM
{
    public class ClientSetting : INotifyPropertyChanged
    {
        private bool _enable = false;

        public event PropertyChangedEventHandler? PropertyChanged;

        [DisplayName("服务器")]
        public string Server { get; set; } = "39.108.140.19";

        [DisplayName("端口")]
        public int Port { get; set; } = 1883;

        [DisplayName("用户名")]
        public string Username { get; set; } = "oitvtbHD4ySR----";

        [DisplayName("密码")]
        public string Password { get; set; } = "Bsv1QVcs0*------";

        [DisplayName("客户端ID")]
        public string ClientID { get; set; } =
            "inspur:oitvtbHD:0344dbbe868f714a7bf310074c4a5b634ce7a1ef";

        [DisplayName("推送Topic")]
        public string PushTopic { get; set; } = "device/inspur/oitvtbHD/report";

        [DisplayName("连接")]
        [JsonIgnore]
        public bool Enable
        {
            get => _enable;
            set
            {
                try
                {
                    ConnentCall?.Invoke(value);
                    _enable = value;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        [ReadOnly(true)]
        [DisplayName("连接状态")]
        [JsonIgnore]
        public bool Status { get; set; } = false;

        [DisplayName("开启业务")]
        public bool Task { get; set; } = true;

        public Action<bool> ConnentCall;

        public virtual string Serialize()
        {
            // 创建 JsonSerializerOptions 对象并设置 WriteIndented 为 true
            var options = new JsonSerializerOptions { WriteIndented = true };
            // 使用 JsonSerializer.Serialize 方法，并传递 options 参数
            string jsonString = JsonSerializer.Serialize(this, options);
            return (jsonString);
        }

        public static ClientSetting Deserialize(string dat)
        {
            return JsonSerializer.Deserialize<ClientSetting>(dat);
        }
    }

    public class ControlSetting : ClientSetting
    {
        [DisplayName("订阅Topic")]
        public string SubTopic { get; set; } = "device/inspur/#";

        public override string Serialize()
        {
            // 创建 JsonSerializerOptions 对象并设置 WriteIndented 为 true
            var options = new JsonSerializerOptions { WriteIndented = true };
            // 使用 JsonSerializer.Serialize 方法，并传递 options 参数
            string jsonString = JsonSerializer.Serialize(this, options);
            return (jsonString);
        }

        public static new ControlSetting Deserialize(string dat)
        {
            return JsonSerializer.Deserialize<ControlSetting>(dat);
        }
    }
}
